$(function () {
    //表格
    var table_dataGrid = $('#table_dataGrid');
    //弹窗
    var input_member = $('#input_member');

    table_dataGrid.datagrid({
        url: "/member/list",
        fit: true,
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xx', checkbox: true},
            {field: 'memberNumber', title: '卡号', width: 100, align: 'center'},
            {
                field: 'memberName',
                title: '会员名称',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    var sex = row.sex == 0 ? "先生" : "女士"
                    return value + "(" + sex + ")";
                }
            },
            {
                field: 'label', title: '标签', width: 50, align: 'center', formatter: function (value, row, index) {
                if (value == 0) {
                    return "";
                } else {
                    return '<img src="/static/images/' + value + 'rank.jpg" alt="' + value + '级" height="20px">';
                }
            }
            },
            {field: 'tel', title: '电话号码', width: 100, align: 'center'},
            {
                field: 'group', title: '分组', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.name : "暂无";
            }
            },
            {
                field: 'grade', title: '会员等级', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.name : "暂无";
            }
            },
            {field: 'integral', title: '可用积分', width: 100, align: 'center'},
            {field: 'balance', title: '储值余额', width: 100, align: 'center'},
            {field: 'address', title: '客户地址', width: 100, align: 'center'},
        ]],
        onLoadSuccess: function (data) {
            var arr1 = [];
            var arr2 = [];
            var arr3 = [];
            $.each(data.rows, function (i, row) {
                arr1[i] = row.memberName;
                arr2[i] = row.balance;
                arr3[i] = row.integral;
            })
            var myChart = echarts.init(document.getElementById('member_Bar_main'));
            var option = {
                title: {
                    text: '员工余额积分树形图',
                    subtext: "当前列表统计",
                    x: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['余额', '积分'],
                    x: 'left',
                    orient: 'vertical'
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    data: arr1
                }],
                yAxis: [{
                    type: 'value'
                }],
                series: [
                    {
                        name: '余额',
                        type: 'bar',
                        data: arr2,
                        markPoint: {
                            data: [{type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}]
                        },
                        markLine: {
                            data: [{type: 'average', name: '平均值'}]
                        }
                    }, {
                        name: '积分',
                        type: 'bar',
                        data: arr3,
                        markPoint: {
                            data: [{type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}]
                        }, markLine: {
                            data: [{type: 'average', name: '平均值'}]
                        }
                    }
                ]
            };
            myChart.setOption(option);
        },
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true //只选择一行
    });



    var cmdObj = {
        //点击add显示添加弹窗
        add: function () {
            input_member.dialog('setTitle', '添加会员信息');

            $("#member_form").form("clear", "none"); //清空
            $("#id").val("");

            $("#grade").combobox("select", 1);      //选择
            $(".group").combobox("select", 4);
            $("#sex").combobox("select", 0);

            $("#add_update_password").show();   //显示密码
            $("#tr_balance").show();   //显示余额
            $("#password").passwordbox("enable", "none");   //可用
            input_member.dialog("center", "none")

            input_member.dialog("open");
        },
        //修改前的弹窗
        update: function () {
            input_member.dialog('setTitle', '修改会员信息');
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            if (row.grade != null) {
                row["grade.id"] = row.grade.id
            }
            if (row.group != null) {
                row["group.id"] = row.group.id
            }
            $("#member_form").form("load", row); //回显
            $("#add_update_password").css("display", "none");   //隐藏
            $("#tr_balance").hide();   //隐藏
            $("#password").passwordbox("disable", "none");   //禁用
            $("#input_member").dialog("open");
        },
        //显示和隐藏 图表 提示
        show: function () {
            $("#member_panel").toggle(1000);
        },
        //添加和修改
        input: function () {
            var str = "添加";
            var url = "/member/save";
            if ($("[name=id]").val()) {
                str = "修改";
                url = "/member/update"
            }
            $("#member_form").form('submit', {
                url: url,
                success: function (data) {
                    data = JSON.parse((data));
                    if (data.success) {
                        $.messager.alert('温馨提示', str + '成功');
                        input_member.dialog('close');
                        table_dataGrid.datagrid("reload");
                    } else {
                        $.messager.alert('温馨提示', str + '失败,原因:' + data.msg);
                    }
                }
            })
        },
        //修改状态为挂失
        updateState: function () {
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            $.messager.confirm('确认', '你确定要挂失会员' + row.memberName + '吗？', function (r) {
                if (r) {
                    $.post("/member/updateState", {id: row.id, state: 1}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '挂失成功');
                            table_dataGrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', '挂失失败,原因:' + data.msg);
                        }
                    });
                }
            });

        },
        //修改状态为删除
        delete: function () {
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            $.messager.confirm('确认', '你确定要删除会员' + row.memberName + '吗？', function (r) {
                if (r) {
                    $.post("/member/updateState", {id: row.id, state: 2}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功');
                            table_dataGrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', '删除失败,原因:' + data.msg);
                        }
                    });
                }
            });

        },
        //补卡
        addState: function () {
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            if (row.state != 1) {
                $.messager.alert('温馨提示', '请选择已挂失的会员信息');
                return;
            }
            $.messager.confirm('确认', '你确定要恢复会员' + row.memberName + '的信息吗？', function (r) {
                if (r) {
                    $.post("/member/updateState", {id: row.id, state: 0}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '恢复成功');
                            table_dataGrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', '恢复失败,原因:' + data.msg);
                        }
                    });
                }
            });
        },
        //查看挂失会员
        queryState: function () {
            table_dataGrid.datagrid('load', {
                state: 1
            })
        },
        //刷新
        refreshList: function () {
            var keyword = $("#keyword").textbox('getText', 'none')
            table_dataGrid.datagrid('load', {
                keyword: keyword
            })
        },
        //取消:关闭弹窗
        cancel: function () {
            input_member.dialog("close");
            $("#move_group").dialog("close");
        },
        //点击移动至分组跳出页面
        moveShow: function () {
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            $("#id").val(row.id);
            $(".group").combobox("select", row.group.id);
            $("#move_group").dialog("center", "none")
            $("#move_group").dialog("open");
        },
        //真-修改分组
        moveGroup: function () {
            var id = $("#id").val();
            $("#update_group_form").form('submit', {
                url: "/member/updateGroup",
                queryParams: {
                    id: id
                },
                success: function (data) {
                    data = JSON.parse((data));
                    if (data.success) {
                        $.messager.alert('温馨提示', '修改成功');
                        input_member.dialog('close');
                        $("#move_group").datagrid("reload");
                    } else {
                        $.messager.alert('温馨提示', '修改失败,原因:' + data.msg);
                    }
                }
            })
        },
        //标签
        updateLabel: function () {
            var row = table_dataGrid.datagrid("getSelected", "none")
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一行数据');
                return;
            }
            $("#id").val(row.id);
            $("#update_label").dialog("center", "none")
            $("#update_label").dialog("open");
        },
    }
    //修改标签
    $(".updateLabel").click(function () {
        var label = $(this).data("label");
        var id = $("#id").val();
        $.post("/member/updateLabel", {'label': label, 'id': id}, function (data) {
            if (data.success) {
                $.messager.alert('温馨提示', '修改成功');
                $("#update_label").dialog('close');
                table_dataGrid.datagrid("reload");
            } else {
                $.messager.alert('温馨提示', '修改失败,原因:' + data.msg);
            }
        });
    })

    //根据关键字查询
    $('#keyword').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function (e) {
                table_dataGrid.datagrid("load", {
                    keyword: $(e.data.target).textbox('getText', 'none')
                })
            }
        }]
    })
    //为所有 a标签中有data-cmd标签的元素绑定点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    //定义添加弹窗
    input_member.dialog({
        width: 600,
        height: 500,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true,
        footer: $("#dialog_panel")
    });
    //修改标签
    $("#update_label").dialog({
        title: "修改标签",
        width: 300,
        height: 200,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true
    });
    //分组
    $("#move_group").dialog({
        width: 300,
        height: 200,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true,
        footer: $("#group_panel")
    });
    //加载会员等级 grade
    $("#grade").combobox({
        url: '/dataDictionaryItem/getItem',
        valueField: 'id',
        textField: 'name',
        width: 200,
        editable: false,
        panelMaxHeight: 200,
        panelHeight: "auto",
        queryParams: {
            sn: 'grade'
        }
    });
    //加载会员分组 group
    $(".group").combobox({
        url: '/dataDictionaryItem/getItem',
        valueField: 'id',
        textField: 'name',
        width: 200,
        editable: false,     //禁止输入
        panelMaxHeight: 200,
        panelHeight: "auto",
        queryParams: {
            sn: 'group'
        }
    });
})